{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">订单号：</label>
                        <div class="layui-input-inline mr0">
                            <input name="order_sn" id="order_sn" class="layui-input" type="text" placeholder="输入订单号"/>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" id="searchBtn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>

                    </div>
                </div>
            </div>

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="options">
    <a class="layui-btn layui-btn-sm layui-btn-xs {:auth('mall.order/get_edit')}" lay-event="edit">订单详情</a>
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs {:auth('mall.order/delete')}" lay-event="del">删除</a>-->
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateUser">
    <input type="checkbox" lay-filter="ckStateUser" value="{{d.userId}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.state==0?'checked':''}}/>
</script>
<script type="text/html" id="tableStateUser1">
    <input type="checkbox" lay-filter="ckStateUser1" value="{{d.id}}" lay-skin="switch"
           lay-text="热门|非热门" {{d.is_top==1?'checked':''}}/>
</script>
<script type="text/html" id="category">
    {{#  if(d.Goods){ }}
    {{d.Goods.title}}
    {{#  } else { }}
    商品不存在
    {{#  } }}
</script>
<script type="text/html" id="user">
    {{#  if(d.User){ }}
    {{d.User.nick_name}}
    {{#  } else { }}
    会员不存在
    {{#  } }}
</script>
<script type="text/html" id="phone">
    {{#  if(d.User){ }}
    {{d.User.phone}}
    {{#  } else { }}
    手机号不存在
    {{#  } }}
</script>
<!-- 表单弹窗 -->


<!-- js部分 -->
{include file='public/footer' /}
<!--<script type="text/javascript" src="/static/public/assets/js/common.js?v=315"></script>-->
<script>
    layui.use(['layer', 'table', 'admin','form'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var table = layui.table;
        //用户表格初始化
        var dataTable =
                table.render({
                    elem: '#tableUser'
                    , autoSort: false
                    , height: 500
                    , url: "{:__url('mall.order/getData')}" //数据接口
                    , page: true //开启分页
                    , cols: [[ //表头
                        {checkbox: true, fixed: true}
                        , {field: 'order_sn', title: '订单编号', sort: true, width: 180}
                        , {field: 'category', title: '商品名称', toolbar: '#category'}
                        , {field: 'user', title: '用户呢称', toolbar: '#user'}
                        , {field: 'phone', title: '用户手机号', toolbar: '#phone'}
                        , {field: 'amount', title: '金额', sort: true, width: 80}
                        ,{
                            title: '状态', templet: function (d) {
                                var strs = ['<span class="layui-badge layui-bg-blue">待成团</span>',
                                    '<span class="layui-badge layui-bg-green">已支付</span>',
                                    '<span class="layui-badge layui-bg-green">退款中</span>',
                                    '<span class="layui-badge layui-bg-red">已退款</span>',
                                    '<span class="layui-badge layui-bg-orange">已评价</span>',
                                    '<span class="layui-badge layui-bg-orange">已领取</span>',
                                    '<span class="layui-badge layui-bg-red">订单失效</span>',
                                ];
                                return strs[d.status];
                            }, align: 'center',width: 150
                        }
                        , {field: 'create_time', title: '创建时间'}
                        , {fixed: 'right', width: 140, align: 'center', title:'预览详情', toolbar: '#options'}
                    ]]
                });


        //监听工具条
        table.on('tool(tableUser)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'del') {
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post("{:__url('delete')}", {
                        _method: 'delete',
                        id: [data.id]
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                obj.del();
//                                insTb.reload({}, 'data');
                            },1000)
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            } else if (layEvent === 'edit') {
 
                showEditModel('查看订单'+data.order_sn,'/mall.order/get_edit?id='+ data.id );

            }
        });

        //按钮批量删除
        $("#listDelete").click(function () {
            var ids = [];
            var hasCheck = table.checkStatus('dataTable');
            var hasCheckData = hasCheck.data;
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            if (ids.length > 0) {
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post("{{ route('mall.order.delete') }}", {
                        _method: 'delete',
                        ids: ids
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                dataTable.reload({page: {curr: 1}});

                            })
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            } else {
                layer.msg('请选择删除项', {icon: 2})
            }
        })

        // 显示表单弹窗
        function showEditModel(title,url) {
            admin.open({
                type: 2,
                offset:'r',
                title: title,
                content: url,
                area: ['500px', '100%'],
                btnAlign: 'c',   // 按钮居中
                success: function (layero, dIndex) {
                    form.render();    // 表单渲染
                },
                yes: function (index, layero) {
                    // 获取弹出层中的form表单元素
                    var formSubmit=layer.getChildFrame('form', index);
                    //获取表单数据
                    var data = {dosubmit:1};
                    var action = formSubmit[0]['action'];
                    var a = formSubmit.serializeArray();
                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    if (data.parent_id == '') {
                        data.parent_id = '0';
                    }

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);

                            dataTable.reload({page: {curr: 1}});

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');

                    return false;

                },
                btn2: function (index, layero) {
                    layer.close(index);
                }


            });
        }

        //搜索
        $("#searchBtn").click(function () {
            var order_sn = $("#order_sn").val();
            dataTable.reload({
                where: {order_sn: order_sn},
                page: {curr: 1}
            })
        })
    })
</script>

</body>
</html>
